package com.runvp.service.biz.service.impl;

import com.runvp.common.build.BuildAppUtil;
import com.runvp.common.build.BuildEnums;
import com.runvp.common.build.model.DownloadListModel;
import com.runvp.common.core.domain.entity.SysDictData;
import com.runvp.common.core.domain.entity.SysDictType;
import com.runvp.service.biz.service.IDictService;
import com.runvp.system.mapper.SysDictDataMapper;
import com.runvp.system.mapper.SysDictTypeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 字典
 *
 * @author runvp
 * @date 2022-09-15
 */
@Service
public class DictServiceImpl implements IDictService {
    @Autowired
    private SysDictTypeMapper dictTypeMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 字典列表
     *
     * @return map
     */
    @Override
    public Map<String, List<DownloadListModel>> dictList(String dictType) {
        Map<String, List<DownloadListModel>> map = new LinkedHashMap<>();
        Map<String, List<SysDictData>> ds = BuildEnums.maps;
        //构建枚举列表
        if(StringUtils.isNotBlank(dictType)) {
            if(ds.containsKey(dictType)){
                map.put(dictType,convertDictList(ds.get(dictType)));
            }
        }else {
            for(Map.Entry<String,List<SysDictData>> entry:ds.entrySet()){
                map.put(entry.getKey(), convertDictList(entry.getValue()));
            }
        }
        //构建字典列表
        buildDictList(map,dictType);
        return map;
    }
    /**
     * 系统公用配置
     *
     * @param key
     * @return map
     */
    @Override
    public Map<String, Object> commonConfig(String key) {
        Map<String, Object> map = new HashMap<>();
        List<DownloadListModel> rs = dictList(key).get(key);
        if (CollectionUtils.isEmpty(rs)) {
            return map;
        }
        for (DownloadListModel model : rs) {
            map.put(model.getDictLabel(), model.getDictValue());
        }
        return map;
    }
    /**
     * 转换对象
     * @param source
     * @return list
     */
    private List<DownloadListModel> convertDictList(List<SysDictData> source){
        return BuildAppUtil.buildDownloadList(source, "dictValue", "dictLabel");
    }
    /**
     * 构建字典列表
     * @param map
     */
    private void buildDictList(Map<String, List<DownloadListModel>> map,String queryType){
        if(map.containsKey(queryType)){
            return;
        }
        List<String> dictType = getDictTypeList();
        if(StringUtils.isNotBlank(queryType)){
            dictType.clear();
            dictType.add(queryType);
        }
        if(CollectionUtils.isEmpty(dictType)){
            return;
        }
        for(String type:dictType){
            map.put(type,convertDictList(getSysDictDataType(type)));
        }
    }
    /**
     * 查询字典类型列表
     * @return list
     */
    private List<String> getDictTypeList(){
        List<String> stringList = new ArrayList<>();
        SysDictType query = new SysDictType();
        query.setStatus("0");
        List<SysDictType> dictTypes = dictTypeMapper.selectDictTypeList(query);
        if(CollectionUtils.isEmpty(dictTypes)){
            return stringList;
        }
        dictTypes.forEach(t->{
            stringList.add(t.getDictType());
        });
        return stringList;
    }

    /**
     * 根据类型查询字典列表
     * @param dictType
     * @return list
     */
    private List<SysDictData> getSysDictDataType(String dictType){
        SysDictData query = new SysDictData();
        query.setStatus("0");
        query.setDictType(dictType);
        List<SysDictData> dictData = dictDataMapper.selectDictDataList(query);
        if(CollectionUtils.isEmpty(dictData)){
            return new ArrayList<>();
        }
        return dictData;
    }


}
